
package webprocessors;

import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 * This will show all tests in a group in html
 * @author Chris
 */
public class webprocessorShowTests extends webprocessor {
    public webprocessorShowTests() {}
    
    @Override
    public String process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String user = getUserName(request);
        QueryStringHandler qsh = new QueryStringHandler(request.getQueryString());
        LinkedList<String> grps = new LinkedList<String>();
        LinkedList<String> tests = new LinkedList<String>();
        String failed = qsh.getElement("failed");
        String failedMsg = "";
        if(failed != null) {
            failedMsg = "<h3>Failed to add group</h3>";
        }
        String view = qsh.getElement("viewall");
        
        if(view == null || view.equals(""))
            view = "0";
        
        if(!UserReader.getTeacherGroups(user, grps)) {
            return "Please add a group";
        }
        
        String grp;
        String test;
        int newInt = (Integer.parseInt(view)+1)%2;
        String vaText = (view.equals("0"))?"view all":"hide all";
        String viewall = "<a href='tests.jsp?viewall="+newInt+"'>"+vaText+"</a>";
        String additional = "<td>Student Scores</td>";
        
        if(Integer.parseInt(view) == 0) {
            additional = "";
        }
        
        String output = failedMsg + "<table border=1><tr><td>Group</td><td>Test</td><td>Scores ("+viewall+")</td>"+
                "<td>Word List</td>"+additional+"</tr>";   
        while(!grps.isEmpty()) {
            grp = grps.removeFirst();
        
            if(!UserReader.getTests(user, grp, tests)) {
                output += "<tr><td>"+ grp +"</td><td>No Tests</td><td></td>"+additional+"</tr>";
                continue;
            }
                        
            while(!tests.isEmpty()) {
                test = tests.removeFirst();
                if(additional.equals("")) {
                    output += "<tr><td>" + grp + "</td><td>"+test+"</td><td><a href='test.jsp?" +
                        "tid=" + test + "&grpid=" + grp + "'>Scores" + "</a></td><td><a href='words.jsp?" +
                        "grpid=" + grp + "&testid="+test+"'>Words</a></td>" +
                        "</a></td></tr>";
                }
                else {
                    output += "<tr><td>" + grp + "</td><td>"+test+"</td><td><a href='test.jsp?" +
                        "tid=" + test + "&grpid=" + grp + "'>Scores" + "</a></td><td><a href='words.jsp?" +
                        "grpid=" + grp + "&testid="+test+"'>Words</a></td>" +
                        "</a></td><td>"+ webprocessors.webprocessorTest.testOutput(user, test, grp) +"</td></tr>";
                }
            }
        }
        output += "</table>";
        
        return output;
    }

}
